package main.firefox;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class DataRange_Reader {

	private TreeMap<Integer, TreeMap<Integer, DataRange>> mapResults = new TreeMap<Integer, TreeMap<Integer, DataRange>>();

	public DataRange_Reader(String filePath) {

		File file = new File(filePath);
		readMapFromCSV(file);

	}

	public void readMapFromCSV(File file) {

		mapResults.clear();
		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = ",";

		DataRange deo;

		try {

			br = new BufferedReader(new FileReader(file));

			boolean firstTime = true;

			while ((line = br.readLine()) != null) {

				String[] values = line.split(cvsSplitBy);

				if (!firstTime) {
					deo = new DataRange();

					deo.setNumberOfReports(Integer.parseInt(values[0]));
					deo.setMinLimit(Integer.parseInt(values[1]));
					deo.setMaxLimit(Integer.parseInt(values[2]));
					deo.setMonth(Integer.parseInt(values[3]));
					deo.setYear(Integer.parseInt(values[4]));

					TreeMap<Integer, DataRange> monthMap = null;
					if (mapResults.get(deo.getYear()) == null) {
						
						
						monthMap=new  TreeMap<Integer, DataRange>();
						monthMap.put(deo.getMonth(), deo);
						
						

					} else {
						
						monthMap=mapResults.get(deo.getYear());
						monthMap.put(deo.getMonth(), deo);

					}
					
					mapResults.put(deo.getYear(),monthMap);
					
					

				}

				firstTime = false;
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public TreeMap<Integer, TreeMap<Integer, DataRange>> getMapResults() {
		return mapResults;
	}

	public void setMapResults(
			TreeMap<Integer, TreeMap<Integer, DataRange>> mapResults) {
		this.mapResults = mapResults;
	}

}
